package com.example.orderassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.cloud.common.exception.TicketException;
import com.woniuxy.entity.ScoreOrder;
import com.example.orderassembly.mapper.ScoreOrderMapper;
import com.example.orderassembly.service.IScoreOrderService;
import com.woniuxy.form.GetOrder;
import com.woniuxy.form.InsertScoreOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 积分订单表 服务实现类
 * </p>
 *
 * @author yang
 * @since 2021-09-21
 */
@Service
public class ScoreOrderServiceImpl extends ServiceImpl<ScoreOrderMapper, ScoreOrder> implements IScoreOrderService {
    @Resource
    ScoreOrderMapper scoreOrderMapper;

    @Override
    public IPage<ScoreOrder> getScoreOrderByUserId(GetOrder getOrder) {
        //通过JWT 获取用户id;
        Integer userId= getOrder.getUserId();
        Page<ScoreOrder> scoreOrderPage = new Page<>(getOrder.getIndexPage(),getOrder.getPageSize());
        QueryWrapper<ScoreOrder> scoreOrderQueryWrapper = new QueryWrapper<>();
        scoreOrderQueryWrapper.between(null!=getOrder.getStartDate()&&null!=getOrder.getEndDate(),"score_order_buy_time",getOrder.getStartDate(),getOrder.getEndDate());
        scoreOrderQueryWrapper.eq("score_order_user_id",userId);
        IPage<ScoreOrder> scoreOrderIPage = scoreOrderMapper.selectPage(scoreOrderPage, scoreOrderQueryWrapper);
        return scoreOrderIPage;
    }

    @Override
    public Boolean insertScoreOrder(InsertScoreOrder insertScoreOrder) {
        ScoreOrder scoreOrder = new ScoreOrder();
        //todo 通过JWT获取用户ID
        BeanUtils.copyProperties(insertScoreOrder,scoreOrder);
        scoreOrder.setScoreOrderStatus(0);
        scoreOrder.setScoreOrderUserId(insertScoreOrder.getUserId());
        int insert = scoreOrderMapper.insert(scoreOrder);
        return insert==1;
    }

    @Override
    public Boolean updateScoreOrderStatus(Integer scoreGoodId, Integer status) {
        //状态 0:未付款 1:已付款 2:已发货 3:物流中 4:已签收  5:订单完成
        ScoreOrder scoreOrder = getBaseMapper().selectById(scoreGoodId);
        Integer oldStatus = scoreOrder.getScoreOrderStatus();
        System.out.println(oldStatus);
        if(oldStatus>=status){
            throw new TicketException("状态错误");
        }
        int update = 0;
        if((oldStatus==0&&status==1)||(oldStatus==1&&status==2)){
            UpdateWrapper<ScoreOrder> scoreOrderUpdateWrapper = new UpdateWrapper<>();
            scoreOrderUpdateWrapper.set("score_order_status",status);
            scoreOrderUpdateWrapper.eq("score_order_id",scoreGoodId);
           update += getBaseMapper().update(null, scoreOrderUpdateWrapper);
        }
        System.out.println(update);
        return update ==1;
    }
}
